package com.taptech.doufu.ui.view.topicview;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.novel.ImageGsonBean;
import com.taptech.doufu.bean.novel.VipTopicBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.StartActivityUtil;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class VipTopicImageView extends LinearLayout{
    private DFHomeBannerBean bean;
    private String title;
    public VipTopicImageView(Context context) {
        super(context);
    }

    public VipTopicImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VipTopicImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private Context context;
    private View lay;
    private ImageView ivPicBg;

    public VipTopicImageView(final Context context, final VipTopicBean model){
        this(context);
        if (model != null && !TextUtils.isEmpty(model.getUrl())) {
            View view = LayoutInflater.from(context).inflate(R.layout.view_booklist_item, null);
            this.context = context;
            lay = view.findViewById(R.id.lay);
            ivPicBg = (ImageView) view.findViewById(R.id.ivPicBg);
            getData(model.getUrl());
            ivPicBg.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengEventUtil.addEvent(context,UmengEventName.VIP_CARD_CONTENT,model.getEventBean());
                    StartActivityUtil.bannerOnclick(getContext(),bean);
                }
            });
            changeDayModelBg();
            setImageHeight();
            title = model.getTitle();
            updateData();
            int width = ScreenUtil.getScreenWidth(context);
            int height = (int) (width/4.0f);
            LayoutParams params = new LayoutParams(width, height);
            params.gravity = Gravity.CENTER;
            addView(view,params);
        }
    }

    private void getData(String url) {
        if (!TextUtils.isEmpty(url)) {
            ApiClient.getInstance().setUseCache(false).getService().getImageData(url)
                    .compose(RxJavaHelper.<ImageGsonBean>observeOnMainThread((Activity) context))
                    .subscribe(new BaseSubscriber<ImageGsonBean>(){
                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            hide();
                        }

                        @Override
                        public void onNext(ImageGsonBean bean) {
                            super.onNext(bean);
                            if (bean != null) {
                                VipTopicImageView.this.bean = bean.getSingleImage();
                                updateData();
                            }else{
                                hide();
                            }
                        }
                    });
        }else {
            hide();
        }
    }

    private void hide() {
        lay.setVisibility(GONE);
    }

    private void setImageHeight() {
        int width =ScreenUtil.getScreenWidth(context)- ScreenUtil.dip2px(context,24);
        int height = (int) (width/4.0f);
        LayoutParams params = new LayoutParams(width, height);
        ivPicBg.setLayoutParams(params);
    }



    private void updateData() {
        if (bean != null && bean.getImages()!=null &&
                bean.getImages().length>0 &&
                bean.getImages()[0]!=null &&
                !TextUtils.isEmpty(bean.getImages()[0].getImgUrl())) {
            GlideUtil.displayRoundImage(context, ivPicBg, bean.getImages()[0].getImgUrl(), 3);
        } else if (bean != null && !TextUtils.isEmpty(bean.getBanner_img())) {
            GlideUtil.displayRoundImage(context, ivPicBg, bean.getBanner_img(), 3,R.drawable.img_default_loading_banner);

        }else{
            hide();
        }
    }



    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        ivPicBg.setAlpha(1.0f);
    }

    private void darkBg() {
        ivPicBg.setAlpha(Constant.imagViewAlpha);
    }
}
